#include"Author.hpp"

int main() {
    try {
        // 初始化Mapper
        AuthorMapper mapper("tcp://112.74.40.147:3306", "root", "Ahqjhqj73@you", "novel_db");
        
        // 创建新作者
        Author newAuthor{
            0, // ID由数据库生成
            "刘慈欣",
            "著名科幻作家，代表作《三体》",
            10, // 等级
            false, // 是否为转录
            0 // 消息ID (表示NULL)
        };
        int authorId = mapper.insertAuthor(newAuthor);
        std::cout << "创建作者成功，ID: " << authorId << std::endl;
        
        // 查询作者
        Author author = mapper.getAuthorById(authorId);
        std::cout << "作者名称: " << author._name << std::endl;
        
        // 更新作者
        author._level = 11;
        author._message_id = 12345; // 更新消息ID
        mapper.updateAuthor(author);
        std::cout << "更新作者成功" << std::endl;
        
        // 按名称搜索作者
        auto authors = mapper.getAuthorsByName("刘");
        std::cout << "找到" << authors.size() << "位作者" << std::endl;
        
        // 按等级查询
        auto levelAuthors = mapper.getAuthorsByLevel(10);
        std::cout << "等级10的作者数量: " << levelAuthors.size() << std::endl;
        
        // 查询所有作者
        auto allAuthors = mapper.getAllAuthors();
        std::cout << "总作者数量: " << allAuthors.size() << std::endl;
        
        // 删除作者
        mapper.deleteAuthor(authorId);
        std::cout << "删除作者成功" << std::endl;
        
    } catch (const sql::SQLException &e) {
        std::cerr << "SQL错误: " << e.what() << " (错误代码: " << e.getErrorCode() << ")" << std::endl;
    } catch (const std::exception &e) {
        std::cerr << "错误: " << e.what() << std::endl;
    }
}